import 'package:loxen/api/http_server/dictionaries.dart';
import 'package:loxen/api/http_util.dart';
import 'package:rxdart/rxdart.dart';
import 'package:loxen/api/http_server/file_change_delete_add_select.dart';
import 'package:loxen/mode/master/file_list_bean.dart';
import 'package:loxen/mode/master/file_loxen.dart';
import 'package:loxen/viewmode/base_view_model.dart';

class FileListViewModel extends BaseViewModel
    with FileChangeDeleteAddSelectMixin {
  final FileType type;
  final String userId;
  int page = 1;
  int pageSize = 12;
  FileListBean _bean;
  List<FileLoxen> _list;

  List<FileLoxen> get list => _list;

  FileListViewModel(this.type, this.userId);
  //    ('1', '灵感'),
  //    ('2', '素材'),
  //    ('3', '采集'),
  //    ('4', '知识'),
  //    ('5', '应用'),
  //    ('6', '书本'),
  String getType() {
    if (type == FileType.INSPIRATION) {
      return '1';
    }
    if (type == FileType.MATERIAL) {
      return '2';
    }
    if (type == FileType.IMAGE) {
      return '3';
    }
    if (type == FileType.KNOWLEDGE) {
      return '4';
    }
    if (type == FileType.APPLY) {
      return '5';
    }
    return '0';
  }

  void createFile(String name, bool isHide, {Function(String error) onError}) {
    fileAdd(name, getType(), isHide).listen((event) {
      if (event != null && event > 0) {
        _list = _list.take(_list.length).toList();
        _list.add(FileLoxen(
          id: event,
          name: name,
          num: 0,
          canEdit: 1,
          cover: '',
          isHide: isHide,
          myHide: isHide,
        ));
        notifyListeners();
      } else {
        if (onError != null) onError.call('创建失败');
      }
    }, onError: (error, stack) {
      print(error);
      print(stack);
      if (error is ServiceException) {
        var map = error.data as Map;
        if (onError != null) onError.call(map['data']['name'].toString());
      }
    });
  }

  void changeFileName(FileLoxen file, String name, bool isHide) {
    Stream<int> data;
    if (name.trim() != file.name) {
      data = fileChangeName(file.id.toString(), name: name, isHide: isHide);
    } else {
      data = fileChangeName(file.id.toString(), isHide: isHide);
    }
    data.listen((event) {
      if (event == 200) {
        file.name = name;
        file.isHide = isHide;
        notifyListeners();
      }
    }, onError: (error, stack) {
      print(error);
      print(stack);
    });
  }

  void changePrivacyAll() {
    Map data = {};
    for (int i = 0; i < _list.length; i++) {
      var a = _list[i];
      data['${a.id}'] = a.myHide;
    }
    setHideAll(data).listen((event) {
      if (event == 200) {
        _list.forEach((element) => element.isHide = element.myHide);
        notifyListeners();
      }
    }, onError: (error, stack) {
      print(error);
      print(stack);
    });
  }

  void deleteFiles() {
    var data = _list.where((element) => element.select == 1).toList();
    if (data.length > 0) {
      String ids = data.map((e) => e.id.toString()).toList().join("_");
      fileDelete(ids).listen((event) {
        if (event == 200) {
          _list = _list.where((element) => element.select == 0).toList();
          notifyListeners();
        }
      }, onError: (error, stack) {
        print(error);
        print(stack);
      });
    }
  }

  void changePrivacy(FileLoxen fileLoxen) {
    fileLoxen.myHide = !fileLoxen.myHide;
    notifyListeners();
  }

  void deleteChange(FileLoxen fileLoxen) {
    if (fileLoxen.select == 1) {
      fileLoxen.select = 0;
    } else {
      fileLoxen.select = 1;
    }
    notifyListeners();
  }

  void change() {
    notifyListeners();
  }

  void loadData() {
    fileSelect(getType(), page: page.toString(), userId: userId)
        .doOnListen(() {
      mState = BaseState.LOADING;
      notifyListeners();
    }).listen((event) {
      _bean = event;
      if (event.data.count > 0) {
        mState = BaseState.CONTENT;
        _list = _bean.data.results;
      } else {
        mState = BaseState.EMPTY;
      }
    }, onError: (error, stack) {
      print(error);
      print(stack);
      mState = BaseState.FAIL;
    }, onDone: () => notifyListeners());
  }

  void nextData() {
    if (_bean.data.next.trim() == '') return;
    page += 1;
    fileSelect(getType(), page: page.toString(), userId: userId).listen(
        (event) {
      _bean = event;
      if (event.data.count > 0) {
        _list = _list.take(_list.length).toList();
        _list.addAll(event.data.results);
        notifyListeners();
      }
    }, onError: (error, stack) {
      print(error);
      print(stack);
    });
  }



  void unSelectAll() {
    _list.forEach((element) {
      element.select = 0;
    });
    notifyListeners();
  }

  void selectAll() {
    _list.forEach((element) {
      if (element.canEdit == 1) {
        element.select = 1;
      }
    });
    notifyListeners();
  }
}
